/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mamesoft.namibiamc.beans;

import com.mamesoft.namibiamc.tools.JDBC;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.ResultSet;
import java.io.Serializable;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author simone
 */
public class LoginBean implements Serializable {

    private String username;
    private String password;
    private int id = -1;
    private boolean render = false;
    private String nome, cognome, role;
    private String subview = "/subviews/login.jsp";

    /**
     * @return the username
     */
    public String getUsername() {
        return username;
    }

    /**
     * @param username the username to set
     */
    public void setUsername(String username) {
        this.username = username;
    }

    /**
     * @return the password
     */
    public String getPassword() {
        return password;
    }

    /**
     * @param password the password to set
     */
    public void setPassword(String password) {
        this.password = password;
    }

    public String logout() {
        ((HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest()).getSession().removeAttribute("role");
        ((HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest()).getSession().invalidate();
        return "logout";
    }
    
    public String verify() {
        String retval = null;
        try {

            Connection connect = null;
            connect = JDBC.connect();

            String q = "SELECT ID FROM operatore WHERE username='" + getUsername() + "' AND password='" + getPassword() + "'";
            ResultSet set = JDBC.executeQuery(q, connect);
            //error ="error"
            if (set.next()) {
                setId(set.getInt("ID"));
                setRender(true);
                setRole("operatore");
                retval = "operatore";
            } else {
                q = "SELECT ID FROM medico WHERE username='" + getUsername() + "' AND password='" + getPassword() + "'";
                set = JDBC.executeQuery(q, connect);
                //error ="error"
                if (set.next()) {
                    setId(set.getInt("ID"));
                    setRender(true);
                    setRole("medico");
                    retval = "medico";
                } else {
                    q = "SELECT ID FROM paziente WHERE username='" + getUsername() + "' AND password='" + getPassword() + "'";
                    set = JDBC.executeQuery(q, connect);
                    if (set.next()) {
                        setId(set.getInt("ID"));
                        setRender(true);
                        setRole("paziente");
                        retval = "paziente";
                    } else {
                        return "login_error";
                    }
                }
            }

            q = "SELECT nome,cognome FROM anagrafe WHERE ID = '" + getId() + "'";
            set = JDBC.executeQuery(q, connect);
            set.next();
            setNome(set.getString("nome").substring(0, 1).toUpperCase() + set.getString("nome").substring(1).toLowerCase());
            setCognome(set.getString("cognome").substring(0, 1).toUpperCase() + set.getString("cognome").substring(1).toLowerCase());

            set.close();
            connect.close();
            
            ((HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest()).getSession().setAttribute("role", role);
                    
            subview = "/subviews/logged_succesfully.jsp";

        } catch (NullPointerException ex) {
            return "login_error";
        } catch (Exception ex) {
            return "database_error";
        }
        return retval;
    }

    /**
     * @return the id
     */
    public int getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(int id) {
        this.id = id;
    }

    /**
     * @return the render
     */
    public boolean isRender() {
        return render;
    }

    /**
     * @param render the render to set
     */
    public void setRender(boolean render) {
        this.render = render;
    }

    /**
     * @return the nome
     */
    public String getNome() {
        return nome;
    }

    /**
     * @param nome the nome to set
     */
    public void setNome(String nome) {
        this.nome = nome;
    }

    /**
     * @return the cognome
     */
    public String getCognome() {
        return cognome;
    }

    /**
     * @param cognome the cognome to set
     */
    public void setCognome(String cognome) {
        this.cognome = cognome;
    }

    /**
     * @return the role
     */
    public String getRole() {
        return role;
    }

    /**
     * @param role the role to set
     */
    public void setRole(String role) {
        this.role = role;
    }

    public boolean isMedico() {
        return getRole().equals("medico");
    }

    public boolean isPaziente() {
        return getRole().equals("paziente");
    }

    public boolean isOperatore() {
        return getRole().equals("operatore");
    }

    /**
     * @return the subview
     */
    public String getSubview() {
        return subview;
    }

    /**
     * @param subview the subview to set
     */
    public void setSubview(String subview) {
        this.subview = subview;
    }
}